const db=require('../config/db.js');
const Sequelize=db.sequlize;
const Article=Sequelize.import('../schema/article.js');
Article.sync({force:false});

class ArticleModel {
  /**创建文章
  *@param data
  *@returns {Promise<*>}
  */
  static async createArticle(data){
    return await Article.create({
      title:data.title,
      author:data.author,
      content:data.content,
      category:data.category
    })
  }

  /**更新文章
  *@param id
  *@param status 事项的状态
  *@returns {Promise<boolean>}
  */
  static async updateArticle(id,data){
     await Article.update({
      title:data.title,
      author:data.author,
      content:data.content,
      category:data.category
    },{
      where:{id},
      fields:['title','author','content','category']
    })
    return true;
  }

  /**获取文章列表
  *@param
  *@returns {Promise<*>}
  */
  static async getArticleList(){
    return await Article.findAndCountAll()
  }

  /**获取文章详情
  *@param id
  *@returns {Promise<Model>}
  */
  static async getArticleDetail(id){
    return await Article.findOne({where:{id}})
  }

  /**删除文章
  *@param id
  *@returns {Promise<boolean>}
  */
  static async deleteArticle(id){
    await Article.destroy({where:{id}})
    return true
  }

}
module.exports = ArticleModel;
